﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KPIS
{
    public static class NullableConverter
    {
        public static int? ToIntNull(object obj)
        {
            int result;
            if (Int32.TryParse((obj == null || obj == DBNull.Value ? new object() : obj).ToString(), out result)) return result;
            return null;
        }
        public static int ToInt(object obj)
        {
            int result;
            Int32.TryParse((obj == null || obj == DBNull.Value ? new object() : obj).ToString(), out result);
            return result;
        }
        public static bool? ToBooleanNull(object obj)
        {
            bool result;
            if (Boolean.TryParse((obj == null || obj == DBNull.Value ? new object() : obj).ToString(), out result)) return result;
            return null;
        }
        public static bool ToBoolean(object obj)
        {
            bool result;
            Boolean.TryParse((obj == null || obj == DBNull.Value ? new object() : obj).ToString(), out result);
            return result;
        }
        public static decimal? ToDecimalNull(object obj)
        {
            decimal result;
            if (Decimal.TryParse((obj == null || obj == DBNull.Value ? new object() : obj).ToString(), out result)) return result;
            return null;
        }
        public static decimal ToDecimal(object obj)
        {
            decimal result;
            Decimal.TryParse((obj == null || obj == DBNull.Value ? new object() : obj).ToString(), out result);
            return result;
        }
        public static double? ToDoubleNull(object obj)
        {
            double result;
            if (Double.TryParse((obj == null || obj == DBNull.Value ? new object() : obj).ToString(), out result)) return result;
            return null;
        }
        public static double ToDouble(object obj)
        {
            double result;
            Double.TryParse((obj == null || obj == DBNull.Value ? new object() : obj).ToString(), out result);
            return result;
        }
        public static DateTime? ToDateTimeNull(object obj)
        {
            DateTime result;
            if (DateTime.TryParse((obj == null || obj == DBNull.Value ? new object() : obj).ToString(), out result)) return result;
            return null;
        }
        public static DateTime ToDateTime(object obj)
        {
            DateTime result;
            DateTime.TryParse((obj ?? new object()).ToString(), out result);
            return result;
        }
        public static string ToStringNull(object obj)
        {
            return obj == null || obj == DBNull.Value ? null : obj.ToString();
        }
        public static string ToString(object obj)
        {
            return obj == null || obj == DBNull.Value ? string.Empty : obj.ToString() ;
        }
    }
}
